package boundary.gui;

import java.awt.Color;

import boundary.UIManager;
import desktop_resources.GUI;
import dto.Dice;
import dto.Field;
import dto.Player;
import dto.PropertyField;
import entity.fields.*;
import entity.fields.properties.Brewery;
import entity.fields.properties.Shipping;
import entity.fields.properties.Street;

public class GUIManager extends UIManager {

	private boolean animationActive = false;
	private desktop_fields.Field[] guiFields;

	@Override
	public int getInteger(String msg) {
		return GUI.getUserInteger(msg);
	}

	@Override
	public boolean getBoolean(String msg, String c1, String c2) {
		return GUI.getUserLeftButtonPressed(msg, c1, c2);
	}

	@Override
	public String getSelectionMultiple(String msg, String... c) {
		return GUI.getUserSelection(msg, c);
	}
	
	@Override
	public String getBooleanMultiple(String msg, String... c) {
		return GUI.getUserButtonPressed(msg, c);
	}

	@Override
	public String getString(String msg) {
		return GUI.getUserString(msg);
	}

	// UI updating methods
	@Override
	public void getMessage(String msg) {
		GUI.showMessage(msg);
	}

	@Override
	public void createPlayers(Player... player) {
		for (int i = 0; i < player.length; i++) {
			createPlayer(player[i]);
		}
	}

	@Override
	public void createPlayer(Player player) {
		GUI.addPlayer(player.getName(), player.getAccount().getBalance());
		System.out.printf("Player created: %s ... %s\n", player.getName(), player.getAccount().getBalance());
	}

	@Override
	public void showPlayers(Player... players) {
		for (int i = 0; i < players.length; i++) {
			GUI.addPlayer(players[i].getName(), 0);
			showPlayer(players[i]);
		}
	}

	@Override
	public void showPlayer(Player player) {
		GUI.setBalance(player.getName(), player.getAccount().getBalance());
		GUI.removeAllCars(player.getName());
		GUI.setCar(player.getFieldIndex() + 1, player.getName());
	}

	@Override
	public void createFields(Field ... fields) {

		guiFields = new desktop_fields.Field[fields.length];
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			Class<?> fieldClass = field.getClass();

			System.out.printf("Field creating: %s ... %s ... %s\n", i, field.getName(), field.getClass());

			// Set Territory field
			if (fieldClass.equals(Street.class)) {
				guiFields[i] = new desktop_fields.Street.Builder().setBgColor(((entity.fields.properties.Street) field).getColor()).build();

			}

			// Set Fleet field
			else if (fieldClass.equals(Shipping.class)) {
				guiFields[i] = new desktop_fields.Shipping.Builder().setBgColor(Color.WHITE).build();

			}

			// Set Tax field
			else if (fieldClass.equals(Tax.class)) {
				guiFields[i] = new desktop_fields.Tax.Builder().setBgColor(Color.BLACK).setFgColor(Color.WHITE).build();

			}

			// Set LaborCamp field
			else if (fieldClass.equals(Brewery.class)) {
				guiFields[i] = new desktop_fields.Brewery.Builder().setBgColor(Color.BLACK).build();

			}

			// Set Refuge field
			else if (fieldClass.equals(Jail.class)) {
				guiFields[i] = new desktop_fields.Jail.Builder().setBgColor(Color.GRAY).build();
			}

			// Set Refuge field
			else if (fieldClass.equals(Start.class)) {
				guiFields[i] = new desktop_fields.Start.Builder().setBgColor(Color.PINK).build();
			}

			// Set Refuge field
			else if (fieldClass.equals(Chance.class)) {
				guiFields[i] = new desktop_fields.Chance.Builder().setBgColor(Color.GREEN).build();
			}

			// Set Free Parking field
			else if (fieldClass.equals(Refuge.class)) {
				guiFields[i] = new desktop_fields.Refuge.Builder().setBgColor(Color.WHITE).build();
			}

			if (!fieldClass.equals(Chance.class)) {
				guiFields[i].setTitle(field.getName());
			}

			guiFields[i].setSubText(field.toString());

			if (field instanceof PropertyField) {
				guiFields[i].setDescription(field.getDescription());
			} else {
				guiFields[i].setDescription(field.toString());
			}
			System.out.printf("Field created.\n");

		}

		GUI.create(guiFields);
	}

	@Override
	public void showFields(Field... fields) {
		for (int i = 0; i < fields.length; i++) {
			if (fields[i] instanceof PropertyField) {
				if (((PropertyField) fields[i]).getOwner() != null)
					GUI.setOwner(i+1, ((PropertyField) fields[i]).getOwner().getName());

				if (fields[i] instanceof PropertyField) {
					guiFields[i].setSubText(fields[i].toString());
					guiFields[i].setDescription(fields[i].getDescription());
					if(fields[i].getClass() == Street.class){
						int buildings = ((Street) fields[i]).getBuildings();
						if(buildings == 5){
							GUI.setHotel(i+1, true);
						}
						else if(buildings < 5 && buildings > 0){
							System.out.println(buildings + "buildings");
							GUI.setHouses(i+1, buildings);
						}
						GUI.setHouses(i+1, 0);
						GUI.setHotel(i+1, false);		
					}
				} else {
					guiFields[i].setDescription(fields[i].toString());
				}
			}
		}
	}

	@Override
	public void showDice(Dice... dices) {
		if (animationActive) {
			int x1, x2, y1, y2;

			int area = 6;
			do {
				x1 = (int) (Math.random() * area) + (area / 2);
				x2 = (int) (Math.random() * area) + (area / 2);
				y1 = (int) (Math.random() * area) + (area / 2);
				y2 = (int) (Math.random() * area) + (area / 2);
			} while (x1 == x2 && y1 == y2 || Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)) > 3);
			DiceAnimation da = new DiceAnimation(dices);
			da.setPositions(x1, x2, y1, y2);
			da.getLoop().run();
			GUI.setDice(dices[0].getValue(), x1, y1, dices[1].getValue(), x2, y2);
		} else
			GUI.setDice(dices[0].getValue(), dices[1].getValue());
	}

	@Override
	public void setBankrupt(Player player) {
		GUI.removeAllCars(player.getName());
	}

	@Override
	public void showCard(String cardText) {
		GUI.displayChanceCard(cardText);
	}

}
